<?php

namespace App\Modules\Admin\Services\Member;

use App\Modules\Admin\Entities\Member\UserScoreModel;
use App\Modules\Admin\Services\BaseService;

class BuyScoreService extends BaseService
{
    public function __construct(UserScoreModel $userModel)
    {
        $this->model = $userModel;
    }
    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('nickname', 'LIKE', '%' . trim($search) . '%');
            }
            if (!empty($uid = $request->input('uid', ''))){
                $query->where('user_id',$uid);
            }
            if (!empty($search = $request->input('search', ''))){
                $query->where('nickname', 'LIKE', '%' . trim($search) . '%');
            }
        };

        return parent::lists($params); // TODO: Change the autogenerated stub
    }
    public function getUsersIntegraDetailLists(array $params) : array
    {
        $uid = $params['uid']??0;
        $waterId = $params['water_id']??"";
        $model = $this->model->with(['water_device_info'])->where(['user_id'=>$uid,'is_delete'=>0]);

        [$startDate,$endDate] =  $params['start_end_time']??["",""];
        if($startDate){
            $model = $model->whereBetween('gift_time', [strtotime($startDate), strtotime($endDate)]);
        }
        if($waterId){
            $model = $model->where(['water_id'=> $waterId]);
        }
        $_model = clone $model;
        $lists = $model->orderBy($this->model->getKeyName() ,'desc')->paginate($this->getLimit($params['limit'] ?? 10));
        $score = $_model->sum('score');
        $sum_score = $_model->sum('sum_score');
        return [
            'current_page' => $lists->currentPage(),
            'count_page' => $lists->lastPage(),
            'per_page' => $lists->perPage(),
            'total' => $lists->total(),
            'data' => $lists->items(),
            'score' => $score,
            'sum_score' => $sum_score,
        ];
    }

    /**
     *  根据用户 查询相应的赠送积分情况
     * @param $params
     * @return mixed[]
     */
    public function getScoreListDetails($params){
        $gift_id = $params['gift_id'];
        $lists = $this->model->with(['water_device_info','user_info'])->where(['gift_id'=>$gift_id,'is_delete'=>0])->get()->toArray();
        return $lists;
    }


    public function getBuyScoreListDetails($params){
        $user_id = $params['user_id'];

        $lists = $this->model->with(['water_device_info','user_info'])->where(['user_id'=>$user_id,'is_delete'=>0])->get()->toArray();
        return $lists;
    }



}
